package com.cennavi.minemap.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cennavi.minemap.bean.DataBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.service.ReportService;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/report")
public class ReportController {

	@Autowired(required = false)
	private ReportService reportService;

	ObjectMapper objectMapper = new ObjectMapper();

	@RequestMapping(value = "/{id}", method = RequestMethod.POST)
	public @ResponseBody MessageBean queryReport(@PathVariable int id, @RequestBody String json) throws Exception {
		MessageBean messageBean = new MessageBean();
		@SuppressWarnings("unchecked")
		Map<String, Object> map = objectMapper.readValue(json, Map.class);
		String startDate = map.get("startDate") + " 00:00:00";
		String endDate = map.get("endDate") + " 23:59:59";
		int soluId = (Integer) map.get("soluId");
		int userId = (Integer) map.get("userId");
		String sql = "";
		Object[] object = null;
		// 按照解决方案分组统计
		if (id == 1) {
			sql = "SELECT soluId, count(*) as cn FROM MM_SOLU_LOG WHERE userId = ? AND isPublish = 1 AND dateTime >= ? AND dateTime <= ? AND soluId = ? GROUP BY soluId";
			if (map.get("isPublish") != null) {
				object = new Object[] { userId, map.get("isPublish"), startDate, endDate, soluId };
			} else {
				sql = sql.replace(" AND isPublish = 1", "");
				object = new Object[] { userId, startDate, endDate, soluId };
			}
		}
		// 按照日期分组统计
		if (id == 2) {
			sql = "SELECT TO_DATE(datetime, 'YYYY-MM-DD') AS DATE, count(1) AS CN FROM "
					+ "MM_SOLU_LOG WHERE userId = ? AND soluId = ? AND dateTime >= ? AND dateTime <= ? AND isPublish = 1 "
					+ "GROUP BY TO_DATE(datetime, 'YYYY-MM-DD') ORDER BY DATE";

			if (map.get("isPublish") != null) {
				object = new Object[] { userId, soluId, startDate, endDate };
			} else {
				sql = sql.replace(" AND isPublish = 1", "");
				object = new Object[] { userId, soluId, startDate, endDate };
			}
		}
		// 按照数据源分组统计
		if (id == 3) {
			sql = "SELECT * FROM (SELECT A.NAME, A.CODE, COALESCE (B.cn, 0) AS CN FROM MM_DATASOURCE A "
					+ "LEFT JOIN (SELECT soluId, SOURCE, COUNT (*) AS cn FROM MM_TILE_LOG "
					+ "WHERE userId = ? AND dateTime >= ? AND dateTime <= ? AND soluId = ? GROUP BY soluId, SOURCE) B "
					+ "ON A .code = B. SOURCE) T ORDER BY CN DESC";
			object = new Object[] { userId, startDate, endDate, soluId };
		}
		// 按照数据日期源分组统计
		if (id == 4) {
			sql = "SELECT TO_DATE(datetime, 'YYYY-MM-DD') AS DATE, COUNT (*) AS cn FROM MM_TILE_LOG "
					+ "WHERE userId = ? AND dateTime >= ? AND dateTime <= ? AND soluId = ? "
					+ "GROUP BY TO_DATE(datetime, 'YYYY-MM-DD') ORDER BY DATE";
			object = new Object[] { userId, startDate, endDate, soluId };
		}

		List<Map<String, Object>> list = reportService.queryReport(sql, object);

		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("upload error");
		} else {
			DataBean<Map<String, Object>> dataBean = new DataBean<Map<String, Object>>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}

		return messageBean;
	}
}
